﻿package com.athena.controller
{	
	import com.athena.vo.Chess;
	import com.athena.process.*;
	import com.adobe.serialization.json.JSON;
	public class Tg_Controller {
		
		function Tg_Controller() { }
		//public static function Solution_Ma(hor : int, ver : int) : Boolean {
			//var type : int = Check_MaChieu(hor, ver) ;
			//if (type == 1) {
				//if (Check_Prevent_Solution(hor - 1, ver + 1) ||  Check_Prevent_Solution(hor - 2, ver + 1)) return false ;
			//} else if (type == 2) {
				//if (Check_Prevent_Solution(hor - 1, ver - 1) ||  Check_Prevent_Solution(hor - 2, ver - 1)) return false ;
			//} else if (type == 3) {
				//if (Check_Prevent_Solution(hor - 1, ver - 1) ||  Check_Prevent_Solution(hor - 1, ver - 2)) return false ;
			//} else if (type == 4) {
				//if (Check_Prevent_Solution(hor + 1, ver - 1) ||  Check_Prevent_Solution(hor + 1, ver - 2)) return false ;
			//} else if (type == 5) {
				//if (Check_Prevent_Solution(hor + 1, ver - 1) ||  Check_Prevent_Solution(hor + 2, ver - 1)) return false ;
			//} else if (type == 6) {
				//if (Check_Prevent_Solution(hor + 1, ver + 1) ||  Check_Prevent_Solution(hor + 2, ver + 1)) return false ;
			//} else if (type == 7) {
				//if (Check_Prevent_Solution(hor + 1, ver + 1) ||  Check_Prevent_Solution(hor + 1, ver + 2)) return false ;
			//} else if (type == 8) {
				//if (Check_Prevent_Solution(hor - 1, ver + 1) ||  Check_Prevent_Solution(hor - 1, ver + 2)) return false;
			//} 
			//return true ;
		//}
		//public static function Solution_Tot(hor : int, ver : int) : Boolean {
			//var type : int = Check_TotChieu(hor, ver) ;
			//if (type == 1)
				//if (Check_Prevent_Solution(hor, ver + 1)) return false ;
			//else if (type == 2)
				//if (Check_Prevent_Solution(hor, ver - 1)) return false ;
			//else if (type == 3)
				//if (Check_Prevent_Solution(hor + 1, ver)) return false ;
			//return true ;
		//}
		//public static function Solution_Xe(hor : int, ver : int) : Boolean {
			//var horTemp : int = hor ;
			//var verTemp : int = ver ;
			//var type : int = Check_XeChieu(hor, ver) ;
			//if (type == 1) {
				//horTemp = hor ;
				//verTemp = ver ;
				//while (horTemp < 9) {
					//horTemp = horTemp + 1 ;					
					//if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) break ;
					//if ((ProcessInTable.ArrCheck[horTemp][verTemp] == 0) && Check_Prevent_Solution(horTemp, verTemp)) return false ;
				//}
			//} else if (type == 2) {
				//horTemp = hor ;
				//verTemp = ver ;
				//while (verTemp < 8) {
					//verTemp = verTemp + 1 ;
					//if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) break ;
					//if ((ProcessInTable.ArrCheck[horTemp][verTemp] == 0) && Check_Prevent_Solution(horTemp, verTemp)) return false ;
				//}
			//} else if (type == 3) {
				//horTemp = hor ;
				//verTemp = ver ;
				//while (verTemp > 0) {
					//verTemp = verTemp - 1 ;
					//if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) break ;
					//if ((ProcessInTable.ArrCheck[horTemp][verTemp] == 0) && Check_Prevent_Solution(horTemp, verTemp)) return false ;
				//}
			//}
			//return true ;
		//}
		
		public static function Search_Symbol(hor : int, ver : int, csCheck : int) {
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Ver == ver)) 
					return ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Symbol ;				
			}
		}
		public static function Check_Run(hor : int, ver : int, huong : int) : Boolean { //huong = 0 - ngang, 1 - doc
			var sym : String = Search_Symbol(hor, ver, ProcessInTable.csOther) ;
			var color : int = ProcessInTable.OtherColor ;
			if (sym == "S") {
				if ((hor == 1) && (ver == 4)) {
					if ((ProcessInTable.ArrCheck[0][3] != color) || (ProcessInTable.ArrCheck[0][5] != color) || (ProcessInTable.ArrCheck[2][3] != color) || (ProcessInTable.ArrCheck[2][5] != color)) return true ;
				} else if ((hor == 0) && (ver == 3)) {
					if (ProcessInTable.ArrCheck[1][4] != color) return true ;
				} else if ((hor == 0) && (ver == 5)) {
					if (ProcessInTable.ArrCheck[1][4] != color) return true ;
				} else if ((hor == 2) && (ver == 3)) {
					if (ProcessInTable.ArrCheck[1][4] != color) return true ;
				} else if ((hor == 2) && (ver == 5)) {
					if (ProcessInTable.ArrCheck[1][4] != color) return true ;
				}				
			} else if (sym == "T") {
				if ((hor == 0) && (ver == 2)) {
					if ((ProcessInTable.ArrCheck[2][0] != color) && (ProcessInTable.ArrCheck[1][1] == 0)) return true ;
					if ((ProcessInTable.ArrCheck[2][4] != color) && (ProcessInTable.ArrCheck[1][3] == 0)) return true ;
				} else if ((hor == 0) && (ver == 6)) {
					if ((ProcessInTable.ArrCheck[2][8] != color) && (ProcessInTable.ArrCheck[1][7] == 0)) return true ;
					if ((ProcessInTable.ArrCheck[2][4] != color) && (ProcessInTable.ArrCheck[1][5] == 0)) return true ;
				} else if ((hor == 2) && (ver == 4)) {
					if ((ProcessInTable.ArrCheck[0][2] != color) && (ProcessInTable.ArrCheck[1][3] == 0)) return true ;
					if ((ProcessInTable.ArrCheck[0][6] != color) && (ProcessInTable.ArrCheck[1][5] == 0)) return true ;
					if ((ProcessInTable.ArrCheck[4][2] != color) && (ProcessInTable.ArrCheck[3][3] == 0)) return true ;
					if ((ProcessInTable.ArrCheck[4][6] != color) && (ProcessInTable.ArrCheck[3][5] == 0)) return true ;
				}
			} else if (sym == "M") {
				//Tren Phai
				if ((hor > 1) && (ver < 8))
					if ((ProcessInTable.ArrCheck[hor - 1][ver] == 0) && (ProcessInTable.ArrCheck[hor - 2][ver + 1] != color)) return true ;
				//Tren Trai
				if ((hor > 1) && (ver > 0)) 
					if ((ProcessInTable.ArrCheck[hor - 1][ver] == 0) && (ProcessInTable.ArrCheck[hor - 2][ver - 1] != color)) return true ;
				//Trai Tren
				if ((hor > 0) && (ver > 1)) 
					if ((ProcessInTable.ArrCheck[hor][ver - 1] == 0) && (ProcessInTable.ArrCheck[hor - 1][ver - 2] != color)) return true ;
				//Trai duoi
				if ((hor < 9) && (ver > 1)) 
					if ((ProcessInTable.ArrCheck[hor][ver - 1] == 0) && (ProcessInTable.ArrCheck[hor + 1][ver - 2] != color)) return true ;
				//Duoi Trai
				if ((hor < 8) && (ver > 0)) 
					if ((ProcessInTable.ArrCheck[hor + 1][ver] == 0) && (ProcessInTable.ArrCheck[hor + 2][ver - 1] != color)) return true ;
				//Duoi Phai
				if ((hor < 8) && (ver < 8)) 
					if ((ProcessInTable.ArrCheck[hor + 1][ver] == 0) && (ProcessInTable.ArrCheck[hor + 2][ver + 1] != color)) return true ;
				//Phai Duoi
				if ((hor < 9) && (ver < 7)) 
					if ((ProcessInTable.ArrCheck[hor][ver + 1] == 0) && (ProcessInTable.ArrCheck[hor + 1][ver + 2] != color)) return true ;
				//Phai Tren
				if ((hor > 0) && (ver < 7)) 
					if ((ProcessInTable.ArrCheck[hor][ver + 1] == 0) && (ProcessInTable.ArrCheck[hor - 1][ver + 2] != color)) return true ;
			} else if (sym == "P") {
				if (huong != 1) {//chieu ngang
					if ((hor > 0) && (ProcessInTable.ArrCheck[hor - 1][ver] != color)) return true ;
					if ((hor < 9) && (ProcessInTable.ArrCheck[hor + 1][ver] != color)) return true ;
					var verTemp : int = ver ;
					var horTemp : int = hor ;
					if (huong == 0) {						
						while (verTemp < 8) {
							verTemp = verTemp + 1 ;
							if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
								while (verTemp < 8) {
									verTemp = verTemp + 1 ;
									if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
										if (ProcessInTable.ArrCheck[horTemp][verTemp] != color) return true;
										break ;
									}
								}
							}
						}
					} else {
						while (verTemp > 0) {
							verTemp = verTemp - 1 ;
							if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
								while (verTemp > 0) {
									verTemp = verTemp - 1 ;
									if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
										if (ProcessInTable.ArrCheck[horTemp][verTemp] != color) return true;
										break ;
									}
								}
							}
						}
					}
				} else {
					if ((ver > 0) && (ProcessInTable.ArrCheck[hor][ver - 1] != color)) return true ;
					if ((ver < 8) && (ProcessInTable.ArrCheck[hor][ver + 1] != color)) return true ;
					var verTemp : int = ver ;
					var horTemp : int = hor ;
					while (horTemp < 9) {
						horTemp = horTemp + 1 ;
						if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
							while (horTemp < 9) {
								horTemp = horTemp + 1 ;
								if (ProcessInTable.ArrCheck[horTemp][verTemp] != 0) {
									if (ProcessInTable.ArrCheck[horTemp][verTemp] != color) return true;
									break ;
								}
							}
						}
					}
				}
			} else if (sym == "X") {
				if (huong != 1) { //chieu ngang
					if ((hor > 0) && (ProcessInTable.ArrCheck[hor - 1][ver] != color)) return true ;
					if ((hor < 9) && (ProcessInTable.ArrCheck[hor + 1][ver] != color)) return true ;
				} else { //Chieu doc
					if ((ver > 0) && (ProcessInTable.ArrCheck[hor][ver - 1] != color)) return true ;
					if ((ver < 8) && (ProcessInTable.ArrCheck[hor][ver + 1] != color)) return true ;
				}
			} else if (sym == "B") {
				if (hor > 4) {
					if ((ver > 0) && (ProcessInTable.ArrCheck[hor][ver - 1] != color)) return true ;
					if ((ver < 8) && (ProcessInTable.ArrCheck[hor][ver + 1] != color)) return true ;
				}
			}
			return false ;
		}
		//public static function Solution_Phao(horMain : int, verMain : int) : Boolean {
			//ProcessInTable.tf.appendText("==>SP:" + horMain + "-" + verMain) ;			
			//var hor : int = horMain ;
			//var ver : int = verMain ;
			//{Check Phao long
			//var type : int = Check_PhaoChieu(horMain, verMain) ;
			//if (type == 1) {
				//while (hor < 9) {
					//hor = hor + 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//if (CheckQuan(hor, ver, "P")) return true ;
						//break ;
					//}
				//}
			//} else if (type == 2) {
				//hor = horMain ;
				//ver = verMain ;
				//while (ver < 8) {
					//ver = ver + 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//if (CheckQuan(hor, ver, "P")) return true ;
						//break ;
					//}
				//}
			//} else if (type == 3) {
				//hor = horMain ;
				//ver = verMain ;
				//while (ver > 0) {
					//ver = ver - 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//if (CheckQuan(hor, ver, "P")) return true ;
						//break ;
					//}
				//}
			//}
			//}
			//ProcessInTable.tf.appendText("==>Pass Phao long") ;
			//{Check Thao nong hoac them con can
			//type = Check_PhaoChieu(horMain, verMain) ;
			//ProcessInTable.tf.appendText("==>type:" + type) ;
			//if (type == 1) {
				//hor = horMain ;
				//ver = verMain ;
				//while (hor < 9) {
					//hor = hor + 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
						//if (Check_Prevent_Solution(hor, ver)) return false ;
					//} else if (ProcessInTable.ArrCheck[hor][ver] == ProcessInTable.OtherColor) {
						//if (Check_Run(hor, ver, 1)) return false ; 
					//}
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//while (hor < 9) {
							//hor = hor + 1 ;
							//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
								//if (Check_Prevent_Solution(hor, ver)) return false ;
							//} else break ;
						//}
					//}
				//}
			//} else if (type == 2) {
				//hor = horMain ;
				//ver = verMain ;
				//while (ver < 8) {
					//ver = ver + 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
						//if (Check_Prevent_Solution(hor, ver)) return false ;
					//} else if (ProcessInTable.ArrCheck[hor][ver] == ProcessInTable.OtherColor) {
						//if (Check_Run(hor, ver, 0)) return false ; 
					//}
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//while (ver < 8) {
							//ver = ver + 1 ;
							//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
								//if (Check_Prevent_Solution(hor, ver)) return false ;
							//} else break ;
						//}
					//}
				//}
			//} else if (type == 3) {
				//hor = horMain ;
				//ver = verMain ;
				//while (ver > 0) {
					//ver = ver - 1 ;
					//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
						//if (Check_Prevent_Solution(hor, ver)) return false ;
					//} else if (ProcessInTable.ArrCheck[hor][ver] == ProcessInTable.OtherColor) {
						//if (Check_Run(hor, ver, 2)) return false ; 
					//}
					//if (ProcessInTable.ArrCheck[hor][ver] != 0) {
						//while (ver > 0) {
							//ver = ver - 1 ;
							//if (ProcessInTable.ArrCheck[hor][ver] == 0) {
								//if (Check_Prevent_Solution(hor, ver)) return false ;
							//} else break ;
						//}
					//}
				//}
			//}
			//}
			//return true ;
		//}		
		
		
		public static function Check_Valid_Run_XePhao(hor : int, ver : int, hor1 : int, ver1 : int) : Boolean {
			if ((hor != hor1) && (ver != ver1)) return false ;
			if (hor == hor1) {
				if (ver > ver1) {
					while ((ver > ver1) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
						ver = ver - 1 ;
					}
				} else {
					while ((ver < ver1) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
						ver = ver + 1 ;
					}
				}
				if (ver == ver1) return true ;
				else return false ;
			} else if (ver == ver1) {
				if (hor > hor1) {
					while ((hor > hor1) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
						hor = hor - 1 ;
					}
				} else {
					while ((hor < hor1) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
						hor = hor + 1 ;
					}
				}
				if (hor == hor1) return true ;
				else return false ;
			}
			return true ;
		}
		
		public static function Check_Prevent_Solution(hor : int, ver : int) : Boolean { //Check xem quan co nao co the di dan (hor,ver)
			//ProcessInTable.tf.appendText("==>Prevent:" + hor + "-" + ver) ;
			var csTuong : int = ProcessInTable.csOther ;
			var horTemp : int = 0 ;
			var verTemp : int = 0 ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Status == 1) continue ;
				//ProcessInTable.tf.appendText("==>Symbol:" + ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol) ;
				if ((ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "X") || (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "P")) {
					if (Check_Valid_Run_XePhao(ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor, ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver, hor, ver)) return true ;
				} else if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "M") {
					//{Check xem Ma co di duoc den nuoc (hor,ver)
					horTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor ;
					verTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver ;
					if ((horTemp > 1) && (verTemp < 8)) {
						if (ProcessInTable.ArrCheck[horTemp - 1][verTemp + 1] == 0) {
							if ((horTemp - 2 == hor) && (verTemp + 1 == ver)) return true ;
						}
					}
					//Tren Trai
					if ((horTemp > 1) && (verTemp > 0)) {
						if (ProcessInTable.ArrCheck[horTemp - 1][verTemp - 1] == 0) {
							if ((horTemp - 2 == hor) && (verTemp - 1 == ver)) return true ;
						}
					}
					//Trai Tren
					if ((horTemp > 0) && (verTemp > 1)) {
						if (ProcessInTable.ArrCheck[horTemp - 1][verTemp - 1] == 0) {
							if ((horTemp - 1 == hor) && (verTemp - 2 == ver)) return true;
						}
					}
					//Trai duoi
					if ((horTemp < 9) && (verTemp > 1)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp - 1] == 0) {
							if ((horTemp + 1 == hor) && (verTemp - 2 == ver)) return true ;
						}
					}
					//Duoi Trai
					if ((horTemp < 8) && (verTemp > 0)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp - 1] == 0) {
							if ((horTemp + 2 == hor) && (verTemp - 1 == ver)) return true ;
						}
					}
					//Duoi Phai
					if ((horTemp < 8) && (verTemp < 8)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp + 1] == 0) {
							if ((horTemp + 2 == hor) && (verTemp + 1 == ver)) return true ;
						}
					}
					//Phai Duoi
					if ((horTemp < 9) && (verTemp < 7)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp + 1] == 0) {
							if ((horTemp + 1 == hor) && (verTemp + 2 == ver)) return true ;
						}
					}
					//Phai Tren
					if ((horTemp > 0) && (verTemp < 7)) {
						if (ProcessInTable.ArrCheck[horTemp-1][verTemp + 1] == 0) {
							if ((horTemp - 1 == hor) && (verTemp + 2 == ver)) return true ;
						}
					}
					//}
				} else if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "S") {
					//{Check xem Sy co di duoc den nuoc (hor,ver)
					horTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor ;
					verTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver ;
					//ProcessInTable.tf.appendText("==>S:" + horTemp + "-" + verTemp) ;
					//Phai Tren
					if (((horTemp == 2) && (verTemp == 3)) || ((horTemp == 1) && (verTemp == 4))) {
						if ((horTemp - 1 == hor) && (verTemp + 1 == ver)) return true ;
					}
					//Trai Tren
					if (((horTemp == 2) && (verTemp == 5)) || ((horTemp == 1) && (verTemp == 4))) {
						if ((horTemp - 1 == hor) && (verTemp - 1 == ver)) return true ;
					}
					//Trai Duoi
					if (((horTemp == 0) && (verTemp == 5)) || ((horTemp == 1) && (verTemp == 4))) {
						if ((horTemp + 1 == hor) && (verTemp - 1 == ver)) return true ;
					}
					//Phai Duoi
					if (((horTemp == 0) && (verTemp == 3)) || ((horTemp == 1) && (verTemp == 4))) {
						if ((horTemp + 1 == hor) && (verTemp + 1 == ver)) return true ;
					}
					//}
				} else if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "T") {
					//{Check xem Tinh co di duoc den nuoc (hor,ver)
					horTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor ;
					verTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver ;
					//Phai Tren
					if (((horTemp == 2) || (horTemp == 4)) && (verTemp < 7)) {
						if (ProcessInTable.ArrCheck[horTemp - 1][verTemp + 1] == 0) {
							if ((horTemp - 2 == hor) && (verTemp + 2 == ver)) return true ;
						}
					}
					//Trai Tren
					if (((horTemp == 2) || (horTemp == 4)) && (verTemp > 1)) {
						if (ProcessInTable.ArrCheck[horTemp - 1][verTemp - 1] == 0) {
							if ((horTemp - 2 == hor) && (verTemp - 2 == ver)) return true ;
						}
					}
					//Trai Duoi
					if (((horTemp == 2) || (horTemp == 0)) && (verTemp > 1)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp - 1] == 0) {
							if ((horTemp + 2 == hor) && (verTemp - 2 == ver)) return true ;
						}
					}
					//Phai Duoi
					if (((horTemp == 2) || (horTemp == 0)) && (verTemp < 7)) {
						if (ProcessInTable.ArrCheck[horTemp + 1][verTemp + 1] == 0) {
							if ((horTemp + 2 == hor) && (verTemp + 2 == ver)) return true ;
						}
					}
					//}
				} else if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "B") {
					//{Check xem Tot co di duoc den nuoc (hor,ver)
					horTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor ;
					verTemp = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver ;
					if (horTemp < 5) {
						if ((horTemp + 1 == hor) && (verTemp == ver)) return true ;
					} else {
						if (verTemp > 0)
							if ((horTemp == hor) && (verTemp - 1 == ver)) return true ;
						if (verTemp < 8)
							if ((horTemp == hor) && (verTemp + 1 == ver)) return true ;
						if (horTemp < 9)
							if ((horTemp + 1 == hor) && (verTemp == ver)) return true ;
					}
					//}
				}				
			}
			return false ;
		}		
		
		public static function CheckChieuTuong() : Boolean {
			var csTuong : int = ProcessInTable.csOther ;
			var csCheck : int = ProcessInTable.currPosition ;
			//ProcessInTable.tf.appendText("==>Start:" + csTuong + "-" + csCheck) ;
			//{Tim toa do tuong
			var hor : int = 0 ;
			var ver : int = 0 ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Status == 1) continue ;
				if (ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Symbol == "Tg") {
					hor = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Hor ;
					ver = ProcessInTable.currTable.ArrPlayers[csTuong].ArrChess[i].Ver ;
				}
			}
			//}
			//ProcessInTable.tf.appendText("==>Tuong:" + hor + "-" + ver + "-" + color) ;
			if (Check_MaChieu(hor, ver, ProcessInTable.MyColor, ProcessInTable.currPosition) != 0) return true ;
			else if (Check_PhaoChieu(hor, ver, ProcessInTable.MyColor, ProcessInTable.currPosition) != 0) return true ;
			else if (Check_XeChieu(hor, ver, ProcessInTable.MyColor, ProcessInTable.currPosition) != 0) return true ;
			else if (Check_TotChieu(hor, ver, ProcessInTable.MyColor, ProcessInTable.currPosition) != 0) return true ;
			return false ;
		}
		
		public static function CheckQuan(hor : int, ver : int, sym : String, csSearch : int) {
			//var csCheck : int = ProcessInTable.currPosition ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csSearch].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csSearch].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[csSearch].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[csSearch].ArrChess[i].Ver == ver)) {
					if (ProcessInTable.currTable.ArrPlayers[csSearch].ArrChess[i].Symbol == sym) return true ;
					else return false ;
				}							
			}
			return false ;
		}
		
		public static function Check_MaChieu(hor : int, ver : int, color : int, csSearch : int) : int {
			//Tren Phai
			if ((hor > 1) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 2][ver + 1] == color) && CheckQuan(hor - 2, ver + 1, "M", csSearch)) return 1 ;
				}
			}
			//Tren Trai
			if ((hor > 1) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 2][ver - 1] == color) && CheckQuan(hor - 2, ver - 1, "M", csSearch)) return 2 ;
				}
			}
			//Trai Tren
			if ((hor > 0) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 1][ver - 2] == color) && CheckQuan(hor - 1, ver - 2, "M", csSearch)) return 3 ;
				}
			}
			//Trai duoi
			if ((hor < 9) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 1][ver - 2] == color) && CheckQuan(hor + 1, ver - 2, "M", csSearch)) return 4 ;
				}
			}
			//Duoi Trai
			if ((hor < 8) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 2][ver - 1] == color) && CheckQuan(hor + 2, ver - 1, "M", csSearch)) return 5 ;
				}
			}
			//Duoi Phai
			if ((hor < 8) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 2][ver + 1] == color) && CheckQuan(hor + 2, ver + 1, "M", csSearch)) return 6 ;
				}
			}
			//Phai Duoi
			if ((hor < 9) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 1][ver + 2] == color) && CheckQuan(hor + 1, ver + 2, "M", csSearch)) return 7 ;
				}
			}
			//Phai Tren
			if ((hor > 0) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor-1][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 1][ver + 2] == color) && CheckQuan(hor - 1, ver + 2, "M", csSearch)) return 8 ;
				}
			}
			return 0 ;
		}
		public static function Check_PhaoChieu(horMain : int, verMain : int, color : int, csSearch : int) : int {
			var hor : int = horMain ;
			var ver : int = verMain ;	
			//{Lentren
			while (hor > 0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					//Cocaro.tf.appendText("=>hor:" + hor) ;
					while (hor > 0) {
						hor = hor - 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "P", csSearch)) return 1 ;
						else if (ProcessInTable.ArrCheck[hor][ver] != 0) break ;
					}
					break ;
				}
			}
			//}
			//{Xuong duoi
			hor = horMain ;
			ver = verMain ;
			while (hor < 9) {
				hor = hor + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (hor < 9) {
						hor = hor + 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "P", csSearch)) return 1 ;
						else if (ProcessInTable.ArrCheck[hor][ver] != 0) break ;
					}
					break ;
				}
			}
			//}
			//{Sang trai
			hor = horMain ;
			ver = verMain ;
			while (ver < 8) {
				ver = ver + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (ver < 8) {
						ver = ver + 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "P", csSearch)) return 2 ;
						else if (ProcessInTable.ArrCheck[hor][ver] != 0) break ;
					}
					break ;
				}
			}
			//}
			//{Sang phai
			hor = horMain ;
			ver = verMain ;
			while (ver > 0) {
				ver = ver - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (ver > 0) {
						ver = ver - 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "P", csSearch)) return 3 ;
						else if (ProcessInTable.ArrCheck[hor][ver] != 0) break ;
					}
					break ;
				}
			}
			//}
			return 0 ;
		}
		public static function Check_XeChieu(horMain : int, verMain : int, color : int, csSearch : int) : int {
			var hor : int = horMain ;
			var ver : int = verMain ;
			//{Len tren
			hor = horMain ;
			ver = verMain ;
			while (hor > 0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "X", csSearch)) return 1 ;
					break ;
				}
			}
			//}
			//{Xuong duoi
			hor = horMain ;
			ver = verMain ;
			while (hor < 9) {
				hor = hor + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "X", csSearch)) return 1 ;
					break ;
				}
			}
			//}
			//{Sang trai
			hor = horMain ;
			ver = verMain ;
			while (ver < 8) {
				ver = ver + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "X", csSearch)) return 2 ;
					break ;
				}
			}
			//}
			//{Sang phai
			hor = horMain ;
			ver = verMain ;
			while (ver > 0) {
				ver = ver - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "X", csSearch)) return 3 ;
					break ;
				}
			}
			//}
			return 0 ;
		}
		public static function Check_TotChieu(hor : int, ver : int, color : int, csSearch : int) : int {
			//Phai
			if ((ver < 8) && (hor < 5)) {
				if ((ProcessInTable.ArrCheck[hor][ver + 1] == color) && CheckQuan(hor, ver + 1, "B", csSearch)) return 1 ;					
			}
			//Trai
			if ((ver > 0) && (hor < 5)) {
				if ((ProcessInTable.ArrCheck[hor][ver-1] == color) && CheckQuan(hor, ver - 1, "B", csSearch)) return 2 ;
			}
			//Duoi
			if (hor < 9) {
				if ((ProcessInTable.ArrCheck[hor + 1][ver] == color) && CheckQuan(hor + 1, ver, "B", csSearch)) return 3 ;
			}
			return 0 ;
		}
		public static function U_Check_TotChieu(hor : int, ver : int, color : int, csSearch : int) : int {
			//Phai
			if ((ver < 8) && (hor < 5)) {
				if ((ProcessInTable.ArrCheck[hor][ver + 1] == color) && CheckQuan(hor, ver + 1, "B", csSearch)) return 1 ;					
			}
			//Trai
			if ((ver > 0) && (hor < 5)) {
				if ((ProcessInTable.ArrCheck[hor][ver-1] == color) && CheckQuan(hor, ver - 1, "B", csSearch)) return 2 ;
			}
			//tren
			if (hor > 0) {
				if ((ProcessInTable.ArrCheck[hor - 1][ver] == color) && CheckQuan(hor - 1, ver, "B", csSearch)) return 3 ;
			}
			return 0 ;
		}
		
		public static function Check_TuongChieu(horMain : int, verMain: int, color : int, csSearch : int) : int {
			var hor : int = horMain ;
			var ver : int = verMain ;
			//{Xuong duoi
			hor = horMain ;
			ver = verMain ;
			while (hor < 9) {
				hor = hor + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "Tg", csSearch)) return 1 ;
					break ;
				}
			}
			//}
			//{Xuong duoi
			hor = horMain ;
			ver = verMain ;
			while (hor >0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if ((ProcessInTable.ArrCheck[hor][ver] == color) && CheckQuan(hor, ver, "Tg", csSearch)) return 1 ;
					break ;
				}
			}
			//}
			return 0 ;
		}
		/*********************** Check xem co bi chieu khong *************************************************/
		public static function Check_LomatTuong() : int {
			var isLomattuong : int = 0 ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess.length; i++) 			
			{
				if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Status == 1) continue ;
				if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Symbol == "Tg") {
					var hor : int = ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Hor ;
					var ver : int = ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Ver ;
					if (Check_TuongChieu(hor, ver, ProcessInTable.OtherColor, ProcessInTable.csOther)) isLomattuong = 1 ;					
					else if (Check_MaChieu(hor, ver, ProcessInTable.OtherColor, ProcessInTable.csOther)) isLomattuong = 2 ;
					else if (Check_PhaoChieu(hor, ver, ProcessInTable.OtherColor, ProcessInTable.csOther)) isLomattuong = 3 ;
					else if (Check_XeChieu(hor, ver, ProcessInTable.OtherColor, ProcessInTable.csOther)) isLomattuong = 4 ;
					else if (U_Check_TotChieu(hor, ver, ProcessInTable.OtherColor, ProcessInTable.csOther)) isLomattuong = 5 ;
				}				
			}			
			return isLomattuong ;
		}
		public static function Search_Chess(hor : int, ver : int) {
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess.length; i++) 
			{
				if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Ver == ver)) {
					ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].gotoAndStop(3) ;
					break ;
				}							
			}
		}
		public static function Valid(obj : Chess) {
			var hor : int = obj.Hor ;
			var ver : int = obj.Ver ;
			// Tren
			if (hor > 7){
				if (ProcessInTable.ArrCheck[hor - 1][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + ver.toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + ver.toString() + "_mc"].over_mc.visible = false ;
				} else if (ProcessInTable.ArrCheck[hor - 1][ver] != obj.Color)
					Search_Chess(hor - 1, ver) ;
			}
			//Phai
			if (ver < 5) {
				if (ProcessInTable.ArrCheck[hor][ver + 1] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + (ver + 1).toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + (ver + 1).toString() + "_mc"].over_mc.visible = false ;
				} else if (ProcessInTable.ArrCheck[hor][ver + 1] != obj.Color)
					Search_Chess(hor, ver + 1) ;					
			}
			//Duoi
			if (hor < 9) {
				if (ProcessInTable.ArrCheck[hor + 1][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + ver.toString() + "_mc"].visible = true ;	
					Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + ver.toString() + "_mc"].over_mc.visible = false ;	
				} else if (ProcessInTable.ArrCheck[hor + 1][ver] != obj.Color)
					Search_Chess(hor + 1, ver) ;
			}
			//Trai
			if (ver > 3) {
				if (ProcessInTable.ArrCheck[hor][ver-1] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + (ver - 1).toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + (ver - 1).toString() + "_mc"].over_mc.visible = false ;
				} else if (ProcessInTable.ArrCheck[hor][ver - 1] != obj.Color)
					Search_Chess(hor, ver - 1) ;
			}
			//Sap mat tuong
			while (hor > 0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					if (ProcessInTable.ArrCheck[hor][ver] != obj.Color) {
						for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess.length; i++) 
						{
							if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Ver == ver)) {
								if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Symbol == "Tg") {
									ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].gotoAndStop(3) ;
									ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.rotation = 180 ;
									ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.x = 0 ;
									ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.y = 15 ;
								}
								break ;
							}							
						}
					}
					break ;
				}
			}
		}
	}
}